package bot.controller;

import bot.mapper.GroupImageTypeMapper;
import bot.pojo.GroupImageType;
import bot.pojo.GroupImageTypeExample;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupImgController {

    private GroupImageTypeExample groupImageTypeExample = new GroupImageTypeExample();
    @Autowired
    private GroupImageTypeMapper groupImageTypeMapper;
    //获取这个群的url参数
    @GetMapping("/getType")
    public String getUrlPram(String groupId){
        groupImageTypeExample.clear();
        groupImageTypeExample.createCriteria().andGroupIdEqualTo(groupId);
        List<GroupImageType> groupImageTypes = groupImageTypeMapper.selectByExample(groupImageTypeExample);
        //判断是否有这个群的配置，没有新增一个
        GroupImageType groupImageType = new GroupImageType();
        if(groupImageTypes.size() == 0){
            groupImageType.setGroupId(groupId);
            Typeclass typeclass = new Typeclass();
            groupImageType.setJsonStr(JSONObject.toJSONString(typeclass));
            groupImageTypeMapper.insert(groupImageType);
            return groupImageType.getJsonStr();
        }else {
             groupImageType = groupImageTypes.get(0);
            return groupImageType.getJsonStr();
        }
    }
    @PostMapping("/updataType")
    public String updataType(@RequestBody UpDataClass upDataClass){
        groupImageTypeExample.clear();
        groupImageTypeExample.createCriteria().andGroupIdEqualTo(upDataClass.getGroupId());
        List<GroupImageType> groupImageTypes = groupImageTypeMapper.selectByExample(groupImageTypeExample);
        if(groupImageTypes.size() == 0){
            return "err";
        }
        GroupImageType groupImageType = groupImageTypes.get(0);
        groupImageType.setJsonStr(upDataClass.getJsonStr());
        groupImageTypeMapper.updateByPrimaryKeySelective(groupImageType);
        return "ok";
    }
}
@Data
class Typeclass{
    private String r18 = "false";//是否R18，默认关闭
    private Integer num = 1 ;//默认单次只返回一张
    private String excludeAI = "false" ; //是否搜索ai作品，默认包含
}
@Data
class UpDataClass{
    private String groupId;
    private String jsonStr;
}